<?php
namespace app\admin\controller;
use function PHPSTORM_META\type;
use think\Db;
use think\Controller;
use think\facade\Request;
use think\Validate;
use app\admin\model\Section as SectionModel;
class Cycle extends Common
{
    /**
     * *****************************************************************************************************************
     */

    /**周期展示
     * @return array|mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */


    public function lists(){

        if (request()->isAjax()) {

            $list = Db::name('cycle')->select();

            if (! empty($list)) {

                foreach ($list as $k => $v) {

                    if($v['id']==1){
                        $list[$k]['start_time'] = '每月'.$v['start_time'].'日';
                        $list[$k]['end_time'] = '每月'.$v['end_time'].'日';
                    }else if($v['id']==2){
                        $start_time = unserialize($v['start_time']);
                        $end_time = unserialize($v['end_time']);
                        $start_time_s = explode('-',$start_time[0]);
                        $start_time_x = explode('-',$start_time[1]);
                        $end_time_s = explode('-',$end_time[0]);
                        $end_time_x = explode('-',$end_time[1]);
                        $list[$k]['start_time'] = '上学期'.$start_time_s[0].'月'.$start_time_s[1].'日 &nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp; '.'下学期'.$start_time_x[0].'月'.$start_time_x[1].'日';
                        $list[$k]['end_time'] = '上学期'.$end_time_s[0].'月'.$end_time_s[1].'日 &nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp; '.'下学期'.$end_time_x[0].'月'.$end_time_x[1].'日';
                    }else if($v['id']==3){
                        $dat_c = explode('-',$v['start_time']);
                        $dat_e = explode('-',$v['end_time']);
                        $list[$k]['start_time'] = '每年'.$dat_c[0].'月'.$dat_c[1].'日';
                        $list[$k]['end_time'] = '每年'.$dat_e[0].'月'.$dat_e[1].'日';
                    }

                    $list[$k]['create_time'] = toDate($v['create_time']);
                    $list[$k]['update_time'] = toDate($v['update_time']);
                }
            }

            return $result = ['code'=>0,'msg'=>'成功!','data'=>$list];


        }

        return $this->fetch('lists');



    }

    /**周期编辑
     * @return mixed
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function cycleEdit(){

        $cycle_id = (int)input('cycle_id');
        if (request()->isAjax()) {
            $data = input('post.');
            if($data['id']==2){
                $data['start_time'] = serialize( $data['start_time']);
                $data['end_time'] = serialize( $data['end_time']);
            }
            $data['update_time'] = time();
            $re = Db::name('cycle')->update($data);
            $log = [
                'content'=>'编辑周期'.$data['title']
            ];
            $wed = new Weblog();
            $wed->insert($log);
            if(!$re){
                $this->error('编辑失败');
            }
            $this->success("编辑成功",'lists');

        }

        $cycle_data = Db::name('cycle')->where('id',$cycle_id)->find();
        if ($cycle_data['id']==2){
            $cycle_data['start_time'] = unserialize($cycle_data['start_time']);
            $cycle_data['end_time'] = unserialize($cycle_data['end_time']);
        }
        $this->assign('info',json_encode($cycle_data,true));
        $this->assign('cycle_data',$cycle_data);

        return $this->fetch('cycleEdit');

    }

    /**修改周期状态
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function  cycleState(){
        $holiday_id=input('post.id');
        $status=input('post.status');

        if (empty($holiday_id)){
            $this->error('周期ID不存在');
        }

        Db::name('cycle')->where('id', $holiday_id)->update(['status'=>$status]);
        $rulename =  Db::name('cycle')->where('id', $holiday_id)->value('title');
        $log = [
            'content'=>'修改周期状态'.$rulename
        ];
        $wed = new Weblog();
        $wed->insert($log);
        $this->success('修改成功');
    }

    /**周期编辑
     * @return mixed
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function cycleAdd(){

        $cycle_id = (int)input('cycle_id');
        if (request()->isAjax()) {
            $data = input('post.');

            $data['update_time'] = time();

            $re = Db::name('cycle')->where('id',$data['id'])->update($data);
            $log = [
                'content'=>'编辑周期'.$data['title']
            ];
            $wed = new Weblog();
            $wed->insert($log);
            if(!$re){
                $this->error('编辑失败');
            }
            $this->success("编辑成功",'lists');

        }

        return $this->fetch('cycleAdd');

    }

}